package com.sviet.game.tetriswindow.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {

	private static final String TAG = DBHelper.class.getSimpleName();

	public static final String DB_NAME = "chetruyen.db";

	public static final int DB_VERSION = 1;

	private final static String[] CREATE_TABLE = { ScoreDao.CREATE_TABLE };

	private final static String DROP_TABLE_HEADER = "DROP TABLE IF EXISTS ";

	private final static String[] DROP_TABLE = { ScoreDao.TABLE_NAME };

	private Context context;

	public DBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.d(TAG, "onCreate()");

		db.beginTransaction();
		try {
			for (String table : CREATE_TABLE) {
				Log.v(TAG, "table=" + table);
				db.execSQL(table);

			}
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d(TAG, "onUpgrade()");

		for (String table : DROP_TABLE) {
			db.execSQL(DROP_TABLE_HEADER + table);
		}

		onCreate(db);
	}

	public void deleteDatabase() {
		this.context.deleteDatabase(DB_NAME);
	}
}
